package device

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/lexkong/log"
	. "service/handler"
	"service/model"
	"service/pkg/errno"
	"service/util"
)

func Register(c *gin.Context) {
	log.Info("User Create function device.")
	var r CreateRequest
	if err := c.Bind(&r); err != nil {
		SendResponse(c, errno.ErrBind, nil)
		return
	}

	fmt.Println("arguments:",r.Product_name)
	d := model.NewDeviceModel(r.Product_name,util.GenShortId(),util.GenShortId())
	// Validate the data.
/*	if err := d.Validate(); err != nil {
		SendResponse(c, errno.ErrValidation, nil)
		return
	}*/
	// Insert the user to the database.
	if err := d.Insert(); err!= nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}

	rsp := CreateResponse{
		ProductName:d.ProductName,
		DeviceName:d.DeviceName,
		Secret:d.Secret,
	}

	// Show the user information.
	SendResponse(c, nil, rsp)

}